package com.yuelu.community.community.controller;

import com.yuelu.community.community.service.AlphaService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.catalina.startup.RealmRuleSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * ClassName: AlphaController
 * Description:
 *
 * @Author 文勇
 * @Create 2024/8/4 21:43
 * @Version 3.0
 */
@Controller
@RequestMapping("/alpha")
public class AlphaController {
    @Autowired
    public AlphaService alphaService;
    @RequestMapping("/hello")
    @ResponseBody
    public String sayhello(){
        return "hello springboot ";
    }

    @RequestMapping("/data")
    @ResponseBody
    public String getData(){
        return  alphaService.find();
    }

    //处理请求的原始方式,不用返回值，交给响应对象
    @RequestMapping("/http")
    public void http(HttpServletRequest Request, HttpServletResponse Response){
        System.out.println(Request.getMethod());
        System.out.println(Request.getServletPath());
        System.out.println(Request.getContextPath());
        //获取请求头信息，遍历输出所有请求头的名称和值。
        Enumeration<String> enumeration = Request.getHeaderNames();//返回一个 Enumeration<String> 类型的对象，该对象包含了所有的请求头名称。
        while (enumeration.hasMoreElements()) {
            String name = enumeration.nextElement();//获取枚举中的下一个元素，即当前请求头的名称。？？
            String value = Request.getHeader(name);
            System.out.println(name + ": " + value);//将请求头的名称和对应的值输出到控制台，以便开发人员可以查看客户端发送的具体请求头信息。
        }
        System.out.println(Request.getParameter("code"));//获取请求体包含的业务数据。名为"code"的请求参数的值。

        // 返回响应数据，设置响应的内容类型为HTML，编码为UTF-8，使用try-with-resources语法确保PrintWriter在使用后自动关闭，避免资源泄露。
        Response.setContentType("text/html;charset=utf-8");
        try (
                //获取输出流
                PrintWriter writer = Response.getWriter();
        ) {  //一行行write出一个个完整网页？这对象太麻烦了
            writer.write("<h1>牛客网</h1>");//向客户端输出HTML内容，这里输出一个大标题"牛客网"。
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //高级的封装
    //处理get请求的两种方式
    //其一，带问号拼接参数
    @RequestMapping(path = "/students", method = RequestMethod.GET)
    @ResponseBody
    public String getStudents(
          @RequestParam(name = "current",required = false,defaultValue = "1") int current,
          @RequestParam(name = "limit",required = false,defaultValue = "10")  int limit
    ){
        System.out.println(current);
        System.out.println(limit);
        return "some students";
    }

    //其二，直接斜杠拼到路径上
    @RequestMapping(value = "/student/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String getstudent(@PathVariable("id") int id){
        System.out.println(id);
        return "a student";
    }

    //处理post请求
    @RequestMapping(value = "/student",method = RequestMethod.POST)
    @ResponseBody
    public String savaStudent(String name,int age){
        System.out.println(name);
        System.out.println(age);
        return "success";
    }

    //响应html数据，不是字符串，所有不用 @ResponseBody注解
    //向浏览器获取老师的数据，get请求
    //方式一，model和View数据都封装到对象中
    @RequestMapping(value = "/teacher",method = RequestMethod.GET)
    public ModelAndView getTeacher(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("name","文勇");  //model的name会给到view.html模板
        modelAndView.addObject("age",80);
        modelAndView.setViewName("/demo/view");
        return modelAndView;
    }

    //方式二，只封装model数据，Views视图直接返回给前端控制器
    //不用 @ResponseBody注解是ying'wei
    @RequestMapping(value = "/school",method = RequestMethod.GET)
    public String getSchool(Model model){
        model.addAttribute("name","湖南工商大学");
        model.addAttribute("age",80);
        return "/demo/view";
    }

    //响应json数据（异步请求，局部验证结果，悄悄访问服务器浏览器页面不刷新）
    @RequestMapping(value = "/emp",method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getEmp(){
        Map<String, Object>  emp= new HashMap<>();
        emp.put("name","文勇");
        emp.put("age",21);
        emp.put("salary","15000");
        return emp;
    }
    
    @RequestMapping(value = "/emps",method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String,Object>> getEmps(){
        List<Map<String,Object>> list = new ArrayList<>();
        HashMap<String, Object> emp = new HashMap<>();
        emp.put("name","文勇");
        emp.put("age",21);
        emp.put("salary",15000);
        list.add(emp);
        emp = new HashMap<>();
        emp.put("name","刘美娜");
        emp.put("age",21);
        emp.put("salary",15000);
        list.add(emp);
        emp = new HashMap<>();
        emp.put("name","郭文洋");
        emp.put("age",21);
        emp.put("salary",15000);
        list.add(emp);
        return list;
    }
}
